import {defineMutation, useMutation} from "@pinia/colada";
import type {Device} from "../types";
import {deleteDevice} from "../api/device.ts";
import {ref} from "vue";
import {ElMessage} from "element-plus"

export const useDeleteDevice = defineMutation(() => {
  const needDeleteDeviceId = ref<number>()
  const {mutate, ...mutation} = useMutation<boolean, number>({
    mutation: (deviceId: number) => deleteDevice(deviceId),
    onSuccess: (res) => {
      if (res) {
        ElMessage.success("删除成功")
      }
    }
  })


  return {
    deleteDeviceFn: (device:Device) => {
      needDeleteDeviceId.value = device.company_device_id
      // 删除标定证书
      // deleteFile(device.company_device_device_img).then(_r => {})
      return mutate(device.company_device_id)
    },
    mutation,
    needDeleteDeviceId
  }
})